package com.ydgg.ltar.api;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ydgg.ltar.model.ArBuilding;
import com.ydgg.ltar.model.ArBuildingContent;
import com.ydgg.ltar.service.ArBuildingContentService;
import com.ydgg.ltar.service.ArBuildingService;

/**
 * 楼内公司
 * 作者： 张叶
 * 创建时间：2018年12月12日  下午4:36:04
 */
@Controller
@RequestMapping("api/arBuildingContent")
public class ArBuildingContentApi {

	@Autowired
	private ArBuildingContentService arBuildingContentService;
	
	@Autowired
	private ArBuildingService arBuildingService;
	
	
	/**
	 * 根据ID，获取大楼简介
	 * 作者： 张叶
	 * 创建时间：2018年12月13日  上午9:20:31
	 */
	@ResponseBody
	@RequestMapping(value = "/getContent")
	public ApiResult listNew(Long abId) {
		ApiResult result = new ApiResult();
		if(abId != null && !"".equals(abId)){
			ArBuilding ar = arBuildingService.selectByPrimaryKey(abId);
			String where = " and status = 'on' and ar_building_id = " + abId;
			where += " order by id";
			List<ArBuildingContent> list = arBuildingContentService.selectByWhere(where);
			if(list.size() > 0){

			    for (ArBuildingContent region : list) {
			        List<ArBuildingContent> children = list.stream().filter(abc1 -> region.getId().equals(abc1.getParentId())).collect(Collectors.toList());
			        region.setChildren(children);
			    }
			    list = list.stream().filter(abc -> abc.getLevel().equals(1)).collect(Collectors.toList());
			    if(!ar.getType().equals("3")){
				    if(list.size() > 4){
				    	list = list.subList(0, 4);
				    }
			    }
				result.data.put("videoUrl", ar.getVideoUrl());
				result.data.put("type", ar.getType());
				result.data.put("name", ar.getName());
				result.data.put("logo", ar.getImageUrl());
				result.data.put("abc", list);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无数据";
			}
		}else{
			result.msg.status = 3;
			result.msg.desc = "参数不能为空";
		}
		return result;
	}	
//	@ResponseBody
//	@RequestMapping(value = "/getContent")
//	public ApiResult listNew(Long abId) {
//		ApiResult result = new ApiResult();
//		if(abId != null && !"".equals(abId)){
//			ArBuilding ar = arBuildingService.selectByPrimaryKey(abId);
//			String where = " and status = 'on' and ar_building_id = " + abId;
//			List<ArBuildingContent> list = arBuildingContentService.selectByWhere(where);
//			if(list.size() > 0){
//				result.data.put("videoUrl", ar.getVideoUrl());
//				result.data.put("type", ar.getType());
//				result.data.put("name", ar.getName());
//				result.data.put("logo", ar.getImageUrl());
//				result.data.put("abc", list);
//				result.msg.status = 0;
//				result.msg.desc = "获取成功";
//			}else{
//				result.msg.status = 1;
//				result.msg.desc = "暂无数据";
//			}
//		}else{
//			result.msg.status = 3;
//			result.msg.desc = "参数不能为空";
//		}
//		return result;
//	}	

}
